package com.ndp.fb.walle.permission;

import com.ndp.fb.rdb.model.User;
import com.ndp.fb.util.DateFormatUtil;
import com.ndp.fb.walle.controller.*;
import com.ndp.fb.walle.permission.validation.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by yeahmobi on 2015/6/8.
 */
@Component
public class PermissionValidation {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired private HttpSession session;

    @Autowired
    AdManageControllerValidation adManageControllerValidation;

    @Autowired
    AdPublishConditionControllerValidation adPublishConditionControllerValidation;

    @Autowired
    AdResourceControllerValidation adResourceControllerValidation;

    @Autowired
    AdwordControllerValidation adwordControllerValidation;

    @Autowired
    ConnectionObjectControllerValidation connectionObjectControllerValidation;

    @Autowired
    EntrustAccountControllerValidation entrustAccountControllerValidation;

    @Autowired
    FBOauthControllerValidation fbOauthControllerValidation;

    @Autowired
    SearchControllerValidation searchControllerValidation;

    @Autowired
    TargetingSpecTemplateControllerValidation targetingSpecTemplateControllerValidation;

    @Autowired
    UserAudienceControllerValidation userAudienceControllerValidation;

    @Autowired
    UserAudienceTemplateControllerValidation userAudienceTemplateControllerValidation;

    @Autowired
    private PublishBatchControllerValidation publishBatchControllerValidation;

    public HttpSession getSession(){
        return session;
    }

    boolean Validation(Class<?> clazz,String methodName,Object[] args){
        boolean result=true;
        User user = (User) session.getAttribute("user");
        Object[] validationMethodArgs=new Object[2];
        validationMethodArgs[0]=user==null?"":user.getTicket();
        validationMethodArgs[1]=args;
        long starttimes=System.currentTimeMillis();
        logger.debug("user : "+validationMethodArgs[0]+",on time : "+ DateFormatUtil.formatLong19(new Date())+" start access Class : "+clazz.getSimpleName()+",Method :"+methodName);
        try {
              Object validation=null;
           if (PublishBatchController.class == clazz) {
                validation = publishBatchControllerValidation;
            }else if (AdManageController.class==clazz){
                validation = adManageControllerValidation;
            }else if (AdPublishConditionController.class==clazz){
                validation =adPublishConditionControllerValidation;
            }else if (AdResourceController.class==clazz){
                validation =adResourceControllerValidation;
            }else if (AdwordController.class==clazz){
                validation =adwordControllerValidation;
            }else if (ConnectionObjectController.class==clazz){
                validation =connectionObjectControllerValidation;
            }else if (EntrustAccountController.class==clazz){
                validation =entrustAccountControllerValidation;
            }else if (FBOauthController.class==clazz){
                validation =fbOauthControllerValidation;
            }else if (SearchController.class==clazz){
                validation =searchControllerValidation;
            }else if (TargetingSpecTemplateController.class==clazz){
                validation =targetingSpecTemplateControllerValidation;
            }else if (UserAudienceController.class==clazz){
                validation =userAudienceControllerValidation;
            }else if (UserAudienceTemplateController.class==clazz){
                validation =userAudienceTemplateControllerValidation;
            }

             if(validation==null){
                 logger.info("Class : " + clazz.getSimpleName() +  " no Need Validation!");
             }else{
                 result = isResult(validation, methodName, result, validationMethodArgs);
             }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        logger.debug("user : "+validationMethodArgs[0]+" use time : "+(System.currentTimeMillis()-starttimes)+"ms Validation is "+ result+" end access Class : "+clazz.getSimpleName()+",Method :"+methodName);
        return result;
    }


    private boolean isResult(Object vaildation, String methodName, boolean result, Object[] validationMethodArgs) {
        Method callmethod= ReflectionUtils.findMethod(vaildation.getClass(), methodName, String.class, Object[].class);
        if(callmethod!=null){
            result = (boolean) ReflectionUtils.invokeMethod(callmethod, vaildation, validationMethodArgs);
        }else {
            logger.debug("Class : " + vaildation.getClass() + ",Method :" + methodName + " no Need Validation!");
        }
        return result;
    }

}
